home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_04 / phillips / freqmtch.c < prev   
C/C++ Source or Header  |  1993-07-01  |  1KB  |  61 lines

  1. /* listing 1 */
  2.  
  3. #include <stdio.h>
  4.  
  5. int freq_match(mask, test)
  6.    char *mask;
  7.    char *test;
  8. {
  9.    static int freq_count[256];   /* the frequency */
  10.                                  /* distribution  */
  11.    int divergence;
  12.    int i;
  13.  
  14.    /* freq_match("maskword", ""); */
  15.    if (test[0] == '\0') {
  16.       /* initialize the distribution array */
  17.       for (i=0; i<256; i++)
  18.          freq_count[i++] = 0;
  19.  
  20.       /* compute the distribution */
  21.       for (i=0; mask[i] != '\0'; i++)
  22.          freq_count[mask[i]] += 1;
  23.  
  24.       /* return a zero for initialization */
  25.       return 0;
  26.    } /* if */
  27.    
  28.    /* freq_match("don't care", "testword"); */
  29.    else {
  30.       /* subtract the freq. dist. of the test word */
  31.       for (i=0; test[i]!='\0'; i++)
  32.          freq_count[test[i]] -= 1;
  33.      
  34.       /* compute the divergence */
  35.       for (divergence=0, i=0; i<256; i++)
  36.          divergence += abs(freq_count[i]);
  37.  
  38.       /* this code is to reset the freq. dist.  */
  39.       /* back to the settings for the mask word */
  40.       for (i=0; test[i]!='\0'; i++)
  41.          freq_count[test[i]] += 1;
  42.  
  43.       return divergence;
  44.    } /* else */
  45. } /* freq_match() */
  46.  
  47. void main()
  48. {
  49.    char mask[80], test[80];
  50.  
  51.    printf("Mask:");
  52.    gets(mask);
  53.  
  54.    printf("Test:");
  55.    gets(test);
  56.  
  57.    freq_match(mask, "");
  58.    printf("The divergence is %d.\n",
  59.           freq_match("", test));
  60. } /* main() */
  61.